import "crvpuppet"



# Definition: at3_xrootd_redir
#
#  Xrootd is a high performance network storage system widely used in high energy 
#  physics experiments such as ATLAS and ALICE. The underlying Xroot data transfer 
#  protocol provides highly efficient access to ROOT based data files. This class provides 
#  the basis for creating a simple Xrootd storage system consisting of one 
#  redirector node and one or more data server nodes.
#  
#  This is the configuration for the ATLAS Tier 3 head node it has a redirector and a
#  data server.
#
# Parameters:
# 	- $webaddr: the web address to the cloud crv server
#   - $roleid: unique identifier of the role for this module
#
# CloudCRV Attributes:
#   - XROOTDInventory: inventory directory for the Simple Service Inventory. 
#      Default value is'/atlas/inventory'    
#   - XROOTDOssLocalRoot: Local root for the data server inventories. Default is'/local/xrootd/a'
#   - XROOTDStoragePath: a directory containing internal Xrootd references to files in the system.
#      Default is '/atlas'
#   - XROOTDFuse - should there be a fuse mount to xrootd (true or false). Default is false.
#   - XROOTDMountPoint - what is the fuse mount point. Default is '/xrootdfs/atlas'
#   
# Actions:
#   - installs xrootd-server.x86_64
#   - sets up the storage directorys, prepares configuration files for xrootd and the
#     the simple storage inventory
#   - starts the xrootd and cmsd services
#
# Requires:
#  
#   
define at3_xrootd_redir ($webaddr="", $roleid="") {
  $fqdnRedirector=$fqdn
  
  
  if $AutoMode==true and $CloudCRV==true {
    $ossLocalRoot=get_role_attr($webaddr,$roleid,"XROOTDOssLocalRoot")
    $storagePath=get_role_attr($webaddr,$roleid,"XROOTDStoragePath")
    $fuse=get_role_attr($webaddr,$roleid,"XROOTDFuse")
    $mountPoint=get_role_attr($webaddr,$roleid,"XROOTDMountPoint") 
    $inventory=get_role_attr($webaddr,$roleid,"XROOTDInventory")
       
  }
  else
  {
	  $inventory='/atlas/inventory'
	  $ossLocalRoot='/local/xrootd/a'
	  $storagePath='/atlas'
	  $fuse=false
	  $mountPoint='/xrootdfs/atlas'
  }
  
  if ( $AutoMode == false ) or ( $AutoMode==true and $CloudCRV==true ) {
    
  }
  
  
  package { ['xrootd-server.x86_64','xrootd-client.x86_64','xrootd-fuse.x86_64','xrootd-debuginfo.x86_64','xrootd-libs.x86_64']:
            ensure => installed,
  }

   

  file { '/etc/sysconfig/xrootd':
    ensure  => present,
    content => template('at3_xrootd_redir/xrootd.tpl'),
    owner   => 'xrootd',
    group   => 'xrootd',
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
  } 
  
  file { "/etc/xrootd/xrootd-clustered.cfg":
    ensure  => present,
    content => template("at3_xrootd_redir/xrootd-clustered.cfg.tpl"),
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
  } 

  #  a directory containing internal Xrootd references to files in the system
  exec { "$storagePath":
    command => "mkdir -p $storagePath",
    path    => ['/bin','/usr/bin'],
    creates => "$storagePath",
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
  }
    
  # The Simple Server Inventory (SSI) provide means to have an inventory for 
  # each data server (See details in xrootd.org/doc/dev/cms_config.pdf). In 
  # order to configure it you will need to run a second instance of xrootd 
  # daemon as well XrdCnsd process that should run on every data server. We 
  # will configure xrootd cluster that consists of two nodes. Host A is a 
  # redirector node that is running the following daemons
  exec { "$inventory":
    command => "mkdir -p $inventory",
    path    => ['/bin','/usr/bin'],
    creates => "$inventory",
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
  }

  # Sets the ownership of the $inventory and $storagePath
  # to the xrootd user
  exec { "chown -R xrootd:xrootd $inventory":
    path    => ['/bin','/usr/bin'],
    logoutput => true,
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"],Exec["$inventory"]],
  }
  exec { "chown -R xrootd:xrootd $storagePath":
    path    => ['/bin','/usr/bin'],
    logoutput => true,
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"],Exec["$storagePath"]],
  }

  # Start the services 
  service { 'xrootd':
    name   => 'xrootd',
    ensure => 'running',
    enable => true,
    pattern => '/usr/bin/xrootd',
    require => [File["/etc/xrootd/xrootd-clustered.cfg"],File['/etc/sysconfig/xrootd'],Exec["$storagePath"],Exec["$inventory"]],
   }

  service { 'cmsd':
    name   => 'cmsd',
    ensure => 'running',
    enable => true,
    require => [File["/etc/xrootd/xrootd-clustered.cfg"],File['/etc/sysconfig/xrootd'],Exec["$storagePath"],Exec["$inventory"]],
   }
   
   # Check to see if a fuse mount is desired
   if $fuse {
     
     # recursively make all the directories for the mount point 
     exec { "$mountPoint":
       command => "mkdir -p $mountPoint",
       creates => "$mountPoint",
       path    => ['/bin','/usr/bin'],
       require => [Service['xrootd']],
       onlyif => ["test -z `mount | grep xrootdfs`"],
       logoutput => true,
     } 
 
     # set the ownership of the directory to the xrootd user
     exec { "chown -R xrootd:xrootd $mountPoint":
       path    => ['/bin','/usr/bin'],
       require => Exec["$mountPoint"],
       logoutput => true,
       onlyif => ["test -z `mount | grep xrootdfs`"],
     } 
 
     exec { 'install-xrootd-fuse':
      command => 'yum -y --disablerepo=epel install xrootd-fuse',
      path => ['/bin','/usr/bin'],
      creates => '/usr/bin/xrootdfs', 
      require => [Service['cmsd'],Service['xrootd']],
      logoutput => true,
     }

     # mount it.
     mount { "$mountPoint":
       atboot   => true,
       device   => "xrootdfs",
       dump => 0,
       ensure   => mounted,
       fstype   => fuse,
       options  => "nonempty,rdr=xroot://${fqdnRedirector}:1094/${storagePath},uid=xrootd",
       pass => 0,
       remounts  => false,
       require  => [Exec['install-xrootd-fuse'],Exec["$mountPoint"]],
     } 

   }
   
   if $AutoMode==true and $CloudCRV==true {
    #publish some variables, e.g. InteractiveNodePublicAddress and let CloudCRV display it on the front.
    set_role_attr($webaddr,$roleid,"XROOTDRedirector","$fqdn")
   }
  
}


define at3_xrootd_redir_reverse($webaddr, $roleid) {
  #set_role_attr($webaddr,$roleid,"ReverseProfileApplied","True")
}

